<?php

defined('BASEPATH') or die('forbidden');

/**
 * 腾讯城市辅助函数
 * @author  Karson
 * @name    city.php
 * @since   2012-5-24 16:48:03
 */
function &get_province_city() {
    static $pc = array();
    if ($pc)
        return $pc;
    $pc = array(11 => array('name' => '北京', 'city' => array(111 => '东城', 1111 => '房山', 1112 => '通州', 1113 => '顺义', 112 => '西城', 1121 => '昌平', 1124 => '大兴', 1126 => '平谷', 1127 => '怀柔', 1128 => '密云', 1129 => '延庆', 115 => '朝阳', 116 => '丰台', 117 => '石景山', 118 => '海淀', 119 => '门头沟',),), 12 => array('name' => '天津', 'city' => array(121 => '和平', 1210 => '东丽', 1211 => '西青', 1212 => '津南', 1213 => '北辰', 122 => '河东', 1221 => '宁河', 1222 => '武清', 1223 => '静海', 1224 => '宝坻', 1225 => '蓟县', 123 => '河西', 124 => '南开', 125 => '河北', 126 => '红桥', 1226 => '滨海新区',),), 13 => array('name' => '河北', 'city' => array(131 => '石家庄', 1310 => '廊坊', 1311 => '衡水', 132 => '唐山', 133 => '秦皇岛', 134 => '邯郸', 135 => '邢台', 136 => '保定', 137 => '张家口', 138 => '承德', 139 => '沧州',),), 14 => array('name' => '山西', 'city' => array(141 => '太原', 1410 => '临汾', 1411 => '吕梁', 142 => '大同', 143 => '阳泉', 144 => '长治', 145 => '晋城', 146 => '朔州', 147 => '晋中', 148 => '运城', 149 => '忻州',),), 15 => array('name' => '内蒙古', 'city' => array(151 => '呼和浩特', 152 => '包头', 1522 => '兴安', 1525 => '锡林郭勒', 1529 => '阿拉善', 153 => '乌海', 154 => '赤峰', 155 => '通辽', 156 => '鄂尔多斯', 157 => '呼伦贝尔', 158 => '巴彦淖尔', 159 => '乌兰察布',),), 21 => array('name' => '辽宁', 'city' => array(211 => '沈阳', 2110 => '辽阳', 2111 => '盘锦', 2112 => '铁岭', 2113 => '朝阳', 2114 => '葫芦岛', 212 => '大连', 213 => '鞍山', 214 => '抚顺', 215 => '本溪', 216 => '丹东', 217 => '锦州', 218 => '营口', 219 => '阜新',),), 22 => array('name' => '吉林', 'city' => array(221 => '长春', 222 => '吉林', 2224 => '延边', 223 => '四平', 224 => '辽源', 225 => '通化', 226 => '白山', 227 => '松原', 228 => '白城',),), 23 => array('name' => '黑龙江', 'city' => array(231 => '哈尔滨', 2310 => '牡丹江', 2311 => '黑河', 2312 => '绥化', 232 => '齐齐哈尔', 2327 => '大兴安岭', 233 => '鸡西', 234 => '鹤岗', 235 => '双鸭山', 236 => '大庆', 237 => '伊春', 238 => '佳木斯', 239 => '七台河',),), 31 => array('name' => '上海', 'city' => array(311 => '黄浦', 3111 => '杨浦', 3112 => '闵行', 3113 => '宝山', 3114 => '嘉定', 3115 => '浦东新', 3116 => '金山', 3117 => '松江', 3126 => '奉贤', 3129 => '青浦', 313 => '卢湾', 3130 => '崇明', 314 => '徐汇', 315 => '长宁', 316 => '静安', 317 => '普陀', 318 => '闸北', 319 => '虹口',),), 32 => array('name' => '江苏', 'city' => array(321 => '南京', 3210 => '扬州', 3211 => '镇江', 3212 => '泰州', 3213 => '宿迁', 322 => '无锡', 323 => '徐州', 324 => '常州', 325 => '苏州', 326 => '南通', 327 => '连云港', 328 => '淮安', 329 => '盐城',),), 33 => array('name' => '浙江', 'city' => array(331 => '杭州', 3310 => '台州', 3311 => '丽水', 332 => '宁波', 333 => '温州', 334 => '嘉兴', 335 => '湖州', 336 => '绍兴', 337 => '金华', 338 => '衢州', 339 => '舟山',),), 34 => array('name' => '安徽', 'city' => array(341 => '合肥', 3410 => '黄山', 3411 => '滁州', 3412 => '阜阳', 3413 => '宿州', 3414 => '巢湖', 3415 => '六安', 3416 => '亳州', 3417 => '池州', 3418 => '宣城', 342 => '芜湖', 343 => '蚌埠', 344 => '淮南', 345 => '马鞍山', 346 => '淮北', 347 => '铜陵', 348 => '安庆',),), 35 => array('name' => '福建', 'city' => array(351 => '福州', 352 => '厦门', 353 => '莆田', 354 => '三明', 355 => '泉州', 356 => '漳州', 357 => '南平', 358 => '龙岩', 359 => '宁德',),), 36 => array('name' => '江西', 'city' => array(361 => '南昌', 3610 => '抚州', 3611 => '上饶', 362 => '景德镇', 363 => '萍乡', 364 => '九江', 365 => '新余', 366 => '鹰潭', 367 => '赣州', 368 => '吉安', 369 => '宜春',),), 37 => array('name' => '山东', 'city' => array(371 => '济南', 3710 => '威海', 3711 => '日照', 3712 => '莱芜', 3713 => '临沂', 3714 => '德州', 3715 => '聊城', 3716 => '滨州', 3717 => '菏泽', 372 => '青岛', 373 => '淄博', 374 => '枣庄', 375 => '东营', 376 => '烟台', 377 => '潍坊', 378 => '济宁', 379 => '泰安',),), 41 => array('name' => '河南', 'city' => array(411 => '郑州', 4110 => '许昌', 4111 => '漯河', 4112 => '三门峡', 4113 => '南阳', 4114 => '商丘', 4115 => '信阳', 4116 => '周口', 4117 => '驻马店', 4118 => '济源', 412 => '开封', 413 => '洛阳', 414 => '平顶山', 415 => '安阳', 416 => '鹤壁', 417 => '新乡', 418 => '焦作', 419 => '濮阳',),), 42 => array('name' => '湖北', 'city' => array(421 => '武汉', 4210 => '荆州', 4211 => '黄冈', 4212 => '咸宁', 4213 => '随州', 422 => '黄石', 4228 => '恩施', 423 => '十堰', 425 => '宜昌', 426 => '襄樊', 427 => '鄂州', 428 => '荆门', 429 => '孝感', 4294 => '仙桃', 4295 => '潜江', 4296 => '天门', '42A21' => '神农架',),), 43 => array('name' => '湖南', 'city' => array(431 => '长沙', 4310 => '郴州', 4311 => '永州', 4312 => '怀化', 4313 => '娄底', 432 => '株洲', 433 => '湘潭', 4331 => '湘西', 434 => '衡阳', 435 => '邵阳', 436 => '岳阳', 437 => '常德', 438 => '张家界', 439 => '益阳',),), 44 => array('name' => '广东', 'city' => array(441 => '广州', 4412 => '肇庆', 4413 => '惠州', 4414 => '梅州', 4415 => '汕尾', 4416 => '河源', 4417 => '阳江', 4418 => '清远', 4419 => '东莞', 442 => '韶关', 4420 => '中山', 443 => '深圳', 444 => '珠海', 445 => '汕头', 4451 => '潮州', 4452 => '揭阳', 4453 => '云浮', 446 => '佛山', 447 => '江门', 448 => '湛江', 449 => '茂名',),), 45 => array('name' => '广西', 'city' => array(451 => '南宁', 4510 => '百色', 4511 => '贺州', 4512 => '河池', 4513 => '来宾', 4514 => '崇左', 452 => '柳州', 453 => '桂林', 454 => '梧州', 455 => '北海', 456 => '防城港', 457 => '钦州', 458 => '贵港', 459 => '玉林',),), 46 => array('name' => '海南', 'city' => array(461 => '海口', 462 => '三亚', 4691 => '五指山', 4692 => '琼海', 4693 => '儋州', 4695 => '文昌', 4696 => '万宁', 4697 => '东方', '46A25' => '定安', '46A26' => '屯昌', '46A27' => '澄迈', '46A28' => '临高', '46A30' => '白沙', '46A31' => '昌江', '46A33' => '乐东', '46A34' => '陵水', '46A35' => '保亭', '46A36' => '琼中', '46A37' => '西沙', '46A38' => '南沙', '46A39' => '中沙',),), 50 => array('name' => '重庆', 'city' => array(501 => '万州', 5010 => '万盛', 5011 => '双桥', 5012 => '渝北', 5013 => '巴南', 502 => '涪陵', 5021 => '长寿', 5022 => '綦江', 5023 => '潼南', 5024 => '铜梁', 5025 => '大足', 5026 => '荣昌', 5027 => '璧山', 5028 => '梁平', 5029 => '城口', 503 => '渝中', 5030 => '丰都', 5031 => '垫江', 5032 => '武隆', 5033 => '忠县', 5034 => '开县', 5035 => '云阳', 5036 => '奉节', 5037 => '巫山', 5038 => '巫溪', 5039 => '黔江', 504 => '大渡口', 5040 => '石柱', 5041 => '秀山', 5042 => '酉阳', 5043 => '彭水', 505 => '江北', 506 => '沙坪坝', 507 => '九龙坡', 508 => '南岸', 5081 => '江津', 5082 => '合川', 5083 => '永川', 5084 => '南川', 509 => '北碚', 5085 => '两江新区',),), 51 => array('name' => '四川', 'city' => array(511 => '成都', 5110 => '内江', 5111 => '乐山', 5113 => '南充', 5114 => '眉山', 5115 => '宜宾', 5116 => '广安', 5117 => '达州', 5118 => '雅安', 5119 => '巴中', 5120 => '资阳', 513 => '自贡', 5132 => '阿坝', 5133 => '甘孜', 5134 => '凉山', 514 => '攀枝花', 515 => '泸州', 516 => '德阳', 517 => '绵阳', 518 => '广元', 519 => '遂宁',),), 52 => array('name' => '贵州', 'city' => array(521 => '贵阳', 522 => '六盘水', 5222 => '铜仁', 5223 => '黔西南', 5224 => '毕节', 5226 => '黔东南', 5227 => '黔南', 523 => '遵义', 524 => '安顺',),), 53 => array('name' => '云南', 'city' => array(531 => '昆明', 5323 => '楚雄', 5325 => '红河', 5326 => '文山', 5328 => '西双版纳', 5329 => '大理', 533 => '曲靖', 5331 => '德宏', 5333 => '怒江', 5334 => '迪庆', 534 => '玉溪', 535 => '保山', 536 => '昭通', 537 => '丽江', 538 => '普洱', 539 => '临沧',),), 54 => array('name' => '西藏', 'city' => array(541 => '拉萨', 5421 => '昌都', 5422 => '山南', 5423 => '日喀则', 5424 => '那曲', 5425 => '阿里', 5426 => '林芝',),), 61 => array('name' => '陕西', 'city' => array(611 => '西安', 6110 => '商洛', 612 => '铜川', 613 => '宝鸡', 614 => '咸阳', 615 => '渭南', 616 => '延安', 617 => '汉中', 618 => '榆林', 619 => '安康',),), 62 => array('name' => '甘肃', 'city' => array(621 => '兰州市', 6210 => '庆阳', 6211 => '定西', 6212 => '陇南', 622 => '嘉峪关', 6229 => '临夏', 623 => '金昌', 6230 => '甘南', 624 => '白银', 625 => '天水', 626 => '武威', 627 => '张掖', 628 => '平凉', 629 => '酒泉',),), 63 => array('name' => '青海', 'city' => array(631 => '西宁', 6321 => '海东', 6322 => '海北', 6323 => '黄南', 6325 => '海南', 6326 => '果洛', 6327 => '玉树', 6328 => '海西',),), 64 => array('name' => '宁夏', 'city' => array(641 => '银川', 642 => '石嘴山', 643 => '吴忠', 644 => '固原', 645 => '中卫',),), 65 => array('name' => '新疆', 'city' => array(651 => '乌鲁木齐', 652 => '克拉玛依', 6521 => '吐鲁番', 6522 => '哈密', 6523 => '昌吉', 6527 => '博尔塔拉', 6528 => '巴音郭楞', 6529 => '阿克苏', 6530 => '克孜勒苏', 6531 => '喀什', 6532 => '和田', 6540 => '伊犁', 6542 => '塔城', 6543 => '阿勒泰', 6591 => '石河子', 6592 => '阿拉尔', 6593 => '图木舒克', 6594 => '五家渠',),), 71 => array('name' => '台湾', 'city' => array(711 => '台北市', 7110 => '桃园县', 7111 => '新竹县', 7112 => '苗栗县', 7113 => '台中县', 7114 => '彰化县', 7115 => '南投县', 7116 => '云林县', 7117 => '嘉义县', 7118 => '台南县', 7119 => '高雄县', 712 => '高雄市', 7120 => '屏东县', 7121 => '澎湖县', 7122 => '台东县', 7123 => '花莲县', 713 => '基隆市', 714 => '台中市', 715 => '台南市', 716 => '新竹市', 717 => '嘉义市', 718 => '台北县', 719 => '宜兰县',),), 81 => array('name' => '香港', 'city' => array('81HCW' => '中西区', '81HEA' => '东区', '81HSO' => '南区', '81HWC' => '湾仔区', '81KKC' => '九龙城区', '81KKT' => '观塘区', '81KSS' => '深水埗区', '81KWT' => '黄大仙区', '81KYT' => '油尖旺区', '81NIS' => '离岛区', '81NKT' => '葵青区', '81NNO' => '北区', '81NSK' => '西贡区', '81NST' => '沙田区', '81NTM' => '屯门区', '81NTP' => '大埔区', '81NTW' => '荃湾区', '81NYL' => '元朗区',),), 82 => array('name' => '澳门', 'city' => array('82ANT' => '圣安多尼堂区', '82CAT' => '大堂区', '82CLN' => '路环', '82LAW' => '望德堂区', '82LAZ' => '风顺堂区', '82OLF' => '花地玛堂区', '82TPA' => '氹仔',),),);
    return $pc;
}

/**
 * 获取省份名称
 */
function province($id = 0) {
    $pc = &get_province_city();
    return isset($pc[$id]) ? $pc[$id]['name'] : $pc[11]['name'];
}

/**
 * 获取城市名称
 */
function city($id = 0) {
    $pc = &get_province_city();
    $province_id = substr($id, 0, 2);
    $province_id = isset($pc[$province_id]) ? $province_id : 11;
    return isset($pc[$province_id]['city'][$id]) ? $pc[$province_id]['city'][$id] : $pc[$province_id]['city'][$province_id . 1];
}

/**
 * 获取省份的ID
 * @param string $name 
 */
function get_province_id($name) {
    $pc = &get_province_city();
    foreach ($pc as $m => $n) {
        if ($n['name'] == $name)
            return $m;
    }
    return 11;
}

/**
 * 获取城市ID
 * @param string $name 
 */
function get_city_id($name) {
    $pc = &get_province_city();
    foreach ($pc as $m => $n) {
        $code = array_search($name, $n['city']);
        if ($code)
            return $code;
    }
    return 111;
}
/**
 * 获取省份数据列表
 * @return array 
 */
function get_province_list() {
    $pc = &get_province_city();
    $province = array();
    foreach ($pc as $m => $n) {
        $province[$m] = $n['name'];
    }
    return $province;
}
/**
 * 获取指定省份的城市列表
 * @param int $province_id 省份ID
 * @return array 
 */
function get_city_list($province_id = 0) {
    $pc = &get_province_city();
    return !isset($pc[$province_id]) ? array() : $pc[$province_id]['city'];
}